package org.nanotek.lucene.index;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.StopAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.nanotek.indexer.util.key.KeyServiceFactory;
import org.nanotek.indexer.util.key.KeyServiceFactoryException;
import org.nanotek.lucene.util.Messages;

public class IndexSet implements IndexBaseSet<UUID> {

	private SortedMap<UUID, IndexWriter> indexMap; 
	private final String indexPath = Messages.getString("FileIndexer.IndexBaseLocation");

	public IndexSet() { 
		indexMap = Collections.synchronizedSortedMap(new TreeMap<UUID,IndexWriter>());
	}

	private  IndexWriter prepareIndex(UUID uuid) throws IOException {
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
		return createIndexWriter( analyzer ,  uuid);
	}

	private IndexWriter prepareStopAnalyzerWriter(UUID uuid) throws IOException {
		Analyzer analyzer = new StopAnalyzer(Version.LUCENE_46);
		return createIndexWriter( analyzer ,  uuid);
	}

	private IndexWriter createIndexWriter(Analyzer analyzer , UUID uuid) throws IOException
	{ 
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46, analyzer);
		File file = new File(indexPath+ "/" + uuid.toString());
		Directory dir = FSDirectory.open(file);
		iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
		IndexWriter writer = new IndexWriter(dir, iwc);
		return writer;
	}

	public synchronized UUID createIndexWriter(IndexType type) throws KeyServiceFactoryException  
	{
		IndexWriter writer;
		UUID key = null; 
		if (indexMap.size() == 0){
			key =  (UUID) new KeyServiceFactory().getKey(UUID.class);
			switch (type){  
			case STANDARD:
				try {
					writer = prepareIndex(key);
					if (writer!=null) { 
						indexMap.put(key, writer);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			case STOPWORD: 
				try {
					writer = prepareStopAnalyzerWriter(key);
					if (writer!=null) {
						indexMap.put(key, writer);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			}
		} else { 
			key = indexMap.firstKey(); 
		}
		return key;
	}

	/*
	 * TODO - Finish key service objects. 
	 */
	public UUID getNextKey() throws KeyServiceFactoryException {
		return (UUID) new KeyServiceFactory().getKey(UUID.class);
	}

	public IndexWriter get(UUID key) {
		return indexMap.get(key);
	}

	public IndexWriter put(UUID  key, IndexWriter value) {
		return indexMap.put(key, value);
	}

	public IndexWriter remove(UUID key) {
		return indexMap.remove(key);
	}

	public void closeIndexes() {
		for (Map.Entry<UUID,IndexWriter> entry: indexMap.entrySet()){ 
			IndexWriter writer = entry.getValue();
			try { 
				writer.close();
			}catch (Exception ex){}
		}
	}
}

